package com.yazi.mxz.server.controller;

import java.util.List;

import javax.annotation.Resource;

import com.fasterxml.jackson.annotation.JsonView;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.yazi.mxz.common.entity.enums.StoreGoodsSellStatus;
import com.yazi.mxz.common.service.StoreGoodsService;
import com.yazi.mxz.server.request.ListRequest;
import com.yazi.mxz.server.response.StoreGoodsListResponse;
import com.yazi.mxz.server.response.StoreGoodsResponse;
import com.yazi.mxz.server.response.view.BaseView;
import com.yazi.mxz.server.response.view.StoreGoodsPicsAStorePromoteView;

import mo.framework.smh.hibernate.PageInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("storeGoodsController")
@RequestMapping("/storeGoods")
public class StoreGoodsController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreGoodsController.class);
	@Resource
	StoreGoodsService storeGoodsService;

	
	@RequestMapping("/list")
	@ResponseBody
	@JsonView(BaseView.class)
	public StoreGoodsListResponse list(@RequestBody ListRequest<StoreGoods> req) {
		log.debug("客户端请求 -> 获取店铺商品列表");
		StoreGoodsListResponse res = new StoreGoodsListResponse();
		try {
			StoreGoods storeGoods = req.getQueryObject();
			storeGoods.setSellStatus(StoreGoodsSellStatus.ON.ordinal());// 在售
			PageInfo pageInfo = req.getPageInfo();
			String searchWord = req.getSearchWord();
			List<StoreGoods> storeGoodsList = storeGoodsService.search(storeGoods, pageInfo, searchWord);
			res.setSuccessData(storeGoodsList);
			res.setPageInfo(pageInfo);
			res.setOk();
		} catch (Exception e) {
			log.error("获取店铺商品列表异常:", e);
			res.setErrorMessage(e.getMessage());
		}
		
		return res;
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	@JsonView(StoreGoodsPicsAStorePromoteView.class)
	public StoreGoodsResponse detail(@RequestBody Long storeGoodsId) {
		log.debug("客户端请求 -> 获取店铺商品详情");
		StoreGoodsResponse res = new StoreGoodsResponse();
		try {
			StoreGoods storeGoods = storeGoodsService.oneById(storeGoodsId);
			if (null != storeGoods && storeGoods.getSellStatus() == StoreGoodsSellStatus.ON.ordinal()) {
				res.setSuccessData(storeGoods);
				res.setOkMessage("ok");
			} else {
				res.setFailMessage("店铺商品不存在或者已经下架");
			}
		} catch (Exception e) {
			log.error("客户端获取店铺商品详情异常:", e);
			res.setErrorMessage(e.getMessage());
		}
		
		return res;
	}
	
	@RequestMapping("/listFromSameOrder")
	@ResponseBody
	@JsonView(BaseView.class)
	public StoreGoodsListResponse listFromSameOrder(@RequestBody ListRequest<StoreGoods> req) {
		log.debug("客户端请求 -> 获取同订单的店铺商品列表");
		StoreGoodsListResponse res = new StoreGoodsListResponse();
		try {
			PageInfo pageInfo = req.getPageInfo();
			List<StoreGoods> storeGoodsList = storeGoodsService.listFromSameOrder(req.getQueryObject().getStoreGoodsId(), pageInfo);
			res.setSuccessData(storeGoodsList);
			res.setOkMessage("ok");
		} catch (Exception e) {
			log.error("客户端获取同订单商品异常:", e);
			res.setErrorMessage(e.getMessage());
		}
		
		return res;
	}
}
